package com.wy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wy.common.Constants;
import com.wy.common.R;
import com.wy.entity.Dict;
import com.wy.entity.Menu;
import com.wy.mapper.DictMapper;
import com.wy.service.IMenuService;
import com.wy.service.IMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王昭贵
 * @since 2023-01-12
 */
@Slf4j
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    private IMenuService menuService;

    @Resource
    private DictMapper dictMapper;

    /****
     * 删除菜单
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        menuService.removeById(id);
        return R.success("删除菜单成功");
    }

    /****
     * 新增 删除菜单
     * @param menu
     * @return
     */
    @PostMapping
    public R save(@RequestBody Menu menu) {
        menuService.saveOrUpdate(menu);
        if(menu.getId()==null){
            return R.success("新增菜单成功");
        }
        return R.success("修改菜单成功");
    }

    /****
     * 获取菜单列表 模糊查询菜单名称
     * @param name
     * @return
     */
    @GetMapping
    public R findAll(@RequestParam(defaultValue = "") String name) {
        return R.success(menuService.findMenus(name));
    }

    /****
     * 获取菜单图标列表
     * @return
     */
    @GetMapping("/icons")
    public R getIcons() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constants.DICT_TYPE_ICON);
        return R.success(dictMapper.selectList(queryWrapper));
    }

    /****
     * 获取当前角色菜单id数组
     * @return
     */
    @GetMapping("/ids")
    public R findAllIds() {
        List<Menu> list = menuService.list();
        ArrayList<Integer> menuId = new ArrayList<>();
        for (Menu menu : list) {
            menuId.add(menu.getId());
        }
        return R.success(menuId);
    }




}

